package com.muyi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.muyi.mapper.CommentMapper;
import com.muyi.mapper.PaperMapper;
import com.muyi.pojo.dto.CommentDTO;
import com.muyi.pojo.dto.PageBean;
import com.muyi.pojo.entity.Comment;
import com.muyi.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author VRMMO
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2025-06-11 15:04:41
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private PaperMapper paperMapper;

//    @Override
//    public List<Comment> list() {
//        return commentMapper.list();
//    }

    @Override
    public void delete(Integer id) {
        commentMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void add(Comment comment) {
        // 增加评论
        commentMapper.insertAll(comment);
        // 增加评论对应的文章的浏览数，浏览数加1
        paperMapper.updateViewCount(comment.getPaper_id());
    }

    @Override
    public void update(Comment comment) {
        commentMapper.update(comment);
    }

    @Override
    public Comment getByCommentId(Integer id) {
        return commentMapper.getByCommentId(id);
    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String content, String paper_title, String nickname) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

        //2. 执行查询
        List<CommentDTO> commentList = commentMapper.list(content, paper_title, nickname);
//        System.out.println(paperMapper.list(paper_title, begin, end, tag_name, category_name));
        Page<CommentDTO> cto = (Page<CommentDTO>) commentList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(cto.getTotal(), cto.getResult());
        return pageBean;
    }




}




